// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Ring Object
// Script Author: Christian Whitehead/Simon Thomley
// Edits by Team ReDream (Tails LP, Xanman) and KarlEmerald
// Elsie was here <3 <3 <3
// ----------------------------------

// Aliases
public value AfterImagesCheck = 0;
public value LightDashCheck = 0;

private alias 10 : TYPE_RING

private alias object.value1 : ring.targetPlayer

private alias 0 : STATE_UNATTRACTED
private alias 1 : STATE_ATTRACTED
private alias 0x1000 : LIGHT_SPEED

// Player aliases
private alias object.state   : player.state
private alias object.xpos    : player.xpos
private alias object.ypos    : player.ypos
private alias object.value0  : player.rings
private alias object.value16 : player.isPlayer2
private alias object.value18 : player.currentPlane
private alias object.value37 : player.shield


private alias 4 : SHIELD_LIGHTNING

// Function declarations
reserve function Ring_DebugDraw
reserve function Ring_DebugSpawn

function Ring_DebugDraw
	DrawSprite(0)
end function


function Ring_DebugSpawn
	CreateTempObject(TypeName[Ring], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
	// Check interaction with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(inputDown.buttonY, 1)
		temp0 = checkResult
		CheckEqual(object[currentPlayer].animation, 28)
		temp0 |= checkResult
		
		//Is the current active ring the closest ring to the player?
		CheckEqual(ringpos, object.entityPos)
		temp0 &= checkResult
		if temp0 == true
			BoxCollisionTest(C_TOUCH, object.entityPos, -25, -25, 25, 25, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == true
				object[currentPlayer].controlMode = -1
				object[currentPlayer].left = 0
				object[currentPlayer].right = 0
				AfterImagesCheck = 1

				foreach (TypeName[Player Object], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].value44 == 1
						object[arrayPos0].state = PlayerObject_LightSpeed_Dash
					end if
				next

				object[currentPlayer].animation = 28
				
				//Trigonometry
				//how far apart horizontally?
				temp1 = object.xpos
				temp1 -= object[currentPlayer].xpos
				temp1 >>= 16

				//how far apart vertically?
				temp0 = object.ypos
				temp0 -= object[currentPlayer].ypos
				temp0 >>= 16
				
				//what angle does that put them at?
				ATan2(temp3, temp1, temp0)
				
				//shoot the player at that angle. towards the ring
				object[currentPlayer].gravity = GRAVITY_AIR
				cos256(object[currentPlayer].xvel, temp3)
				sin256(object[currentPlayer].yvel, temp3)
				object[currentPlayer].xvel *= LIGHT_SPEED
				object[currentPlayer].yvel *= LIGHT_SPEED				
				object[currentPlayer].speed = object[currentPlayer].xvel
				if object[currentPlayer].xvel < 0
					object[currentPlayer].direction = FLIP_X
				else
					object[currentPlayer].direction = FLIP_NONE
				end if
			else
				object[currentPlayer].controlMode = 0	
			end if
		end if


		// Make sure that the player isn't supposed to ignore rings right now
		CheckEqual(player[0].state, PlayerObject_Knockback)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].state, PlayerObject_Knockback)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].state, PlayerObject_Hurt)
		temp0 |= checkResult

		if temp0 == false
			// All checks passed, see if the player is touching the ring

			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == true
				// Player is touching ring - collect it	

				// Turn this object into a sparkle
				player.score += 10
				object.type = TypeName[Ring Sparkle]

				// Set this object to the same drawOrder as the player
				object.drawOrder = player[currentPlayer].currentPlane

				// Add it to the player's ring count

				if player[currentPlayer].isPlayer2 == true

					// P2 Tails in normal gameplay (not VS)
					// So add it to P1's ring count
					player[0].rings++
					if player[0].rings > 999
						player[0].rings = 999
					end if

				else

					// Either:
					// A) P2 in VS, or
					// B) P1 in any mode
					// In any case, give the ring to the player that collected it
					player[currentPlayer].rings++
					if player[currentPlayer].rings > 999
						player[currentPlayer].rings = 999
					end if

				end if

				// Check if the player should get an extra life
				if player[0].rings >= ringExtraLife

					// Not in time attack?
					if options.gameMode != 2
						// Give the player an extra life and play the music
						//player.lives++
						//PlaySfx(SfxName[Life], 0)
						//PauseMusic()
						//ResetObjectEntity(25, TypeName[Music Event], 2, 0, 0)
						//object[25].priority = PRIORITY_ACTIVE
					end if

					ringExtraLife += 100
					if ringExtraLife > 300
						ringExtraLife = 1000
					end if
				end if

				if options.vsMode == true
					if vs.playerID == 0
						if currentPlayer == 0
							vs.totalRings1P++
						else
							vs.totalRings2P++
						end if
					else
						if currentPlayer == 1
							vs.totalRings1P++
						else
							vs.totalRings2P++
						end if
					end if
				end if

				// Play the ring sound
				if ringPan == 0
					PlaySfx(SfxName[Ring L], 0)
					SetSfxAttributes(SfxName[Ring L], -1, -100)
					ringPan = 1
				else
					PlaySfx(SfxName[Ring R], 0)
					SetSfxAttributes(SfxName[Ring R], -1, 100)
					ringPan = 0
				end if
			else
				// Check if the ring should get attracted
				if object.state == STATE_UNATTRACTED
					if player[currentPlayer].shield == SHIELD_LIGHTNING
						BoxCollisionTest(C_TOUCH, object.entityPos, -64, -64, 64, 64, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == true
							// Make the ring attracted to the player
							object.state = STATE_ATTRACTED
							ring.targetPlayer = currentPlayer
						end if
					end if
				end if
			end if
		end if
	next

	if object.state == STATE_ATTRACTED
		arrayPos0 = ring.targetPlayer

		if player[arrayPos0].shield != SHIELD_LIGHTNING
			// If the player lost their shield, then make this ring dropped
			object.type = TypeName[Lose Ring]
			object.animationSpeed = 128
			object.alpha = 256
		else
			arrayPos0 = ring.targetPlayer

			// Move closer to the player
			if object.xpos > player[arrayPos0].xpos
				if object.xvel > 0
					object.xvel -= 0xC000
				else
					object.xvel -= 0x3000
				end if
			else
				if object.xvel < 0
					object.xvel += 0xC000
				else
					object.xvel += 0x3000
				end if
			end if

			if object.ypos > player[arrayPos0].ypos
				if object.yvel > 0
					object.yvel -= 0xC000
				else
					object.yvel -= 0x3000
				end if
			else
				if object.yvel < 0
					object.yvel += 0xC000
				else
					object.yvel += 0x3000
				end if
			end if

			object.xpos += object.xvel
			object.ypos += object.yvel
		end if
	end if
end event


event ObjectDraw

	// All rings use the same frame
	// (Animation is handled by Stage Setup object)
	DrawSprite(ringFrame)

end event


event ObjectStartup

	LoadSpriteSheet("Global/Items.gif")

	// Set each ring's draw order to 4
	foreach (TypeName[Ring], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next

	// Ring frames
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	SpriteFrame(-8, -8, 16, 16, 1, 18)
	SpriteFrame(-8, -8, 16, 16, 1, 35)
	SpriteFrame(-8, -8, 16, 16, 1, 52)
	SpriteFrame(-8, -8, 16, 16, 1, 69)
	SpriteFrame(-8, -8, 16, 16, 1, 86)
	SpriteFrame(-8, -8, 16, 16, 1, 103)
	SpriteFrame(-8, -8, 16, 16, 1, 120)

	// Add the Ring to the debug mode object list
	SetTableValue(TypeName[Ring], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Ring_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Ring_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
